package com.txh.blog.controller;


import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.txh.blog.bean.*;
import com.txh.blog.service.BlogService;
import com.txh.blog.service.TagsService;
import com.txh.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController{
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagsService tagsService;
    @RequestMapping("/bgm")
    public String bgm(){
        return "bgm";
    }
    @RequestMapping("/")
    public String index(@RequestParam(name = "page",required = true,defaultValue = "1") int page,
                        @RequestParam(name = "pageSize",required = true,defaultValue = "2")int pageSize, Model mv){
        List<FirstPageBlog> list = blogService.getFirstPageBlog(page, pageSize);
        List<Type> types = typeService.listTypeSize();
        List<Blog> blogs = blogService.finByTagId();
        List<Tags> tags = tagsService.getAllTag();
        List<getAllRecommendBlog> allRecommendBlog = blogService.getAllRecommendBlog();
        PageInfo<FirstPageBlog> pageInfo=new PageInfo<>(list);
        mv.addAttribute("pageInfo",pageInfo);
        mv.addAttribute("type",types);
        mv.addAttribute("tags",tags);
        mv.addAttribute("recommendBlog",allRecommendBlog);
        return "index";
    }
    @RequestMapping("/search")
    public String search(@RequestParam(name = "page",required = true,defaultValue = "1") int page,
                               @RequestParam(name = "pageSize",required = true,defaultValue = "10")int pageSize,@RequestParam String query,Model mv){
        List<FirstPageBlog> searchBlog = blogService.getSearchBlog(page, pageSize, query);
        PageInfo<FirstPageBlog> PageInfo=new PageInfo<>(searchBlog);
//        List<RecommendBlog> recommendedBlog = blogService.getRecommendedBlog();
        mv.addAttribute("pageInfo",PageInfo);
        return "search";
    }
    @RequestMapping("/blog/{id}")
    public String blog(@PathVariable Long id, HttpSession session,Model mv){

        Blog detailedBlog = blogService.getDetailedBlog(id);
        mv.addAttribute("blog",detailedBlog);
        return "blog";
    }
}
